package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShoppingMapper;
import com.sky.service.UserShoppingService;
import com.sky.vo.SetmealVO;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
@Slf4j
@Service
public class UserShoppingServicelmpl implements UserShoppingService {
    @Autowired
    private UserShoppingMapper userShoppingMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 向购物车添加食物
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("{}", shoppingCart);
        List<ShoppingCart> cart = userShoppingMapper.Select(shoppingCart);
        //先判断购物车是否有相同菜或者套餐 有则把数量加一
        if (cart != null && cart.size() > 0) {
            //有相同的把数量加一
            ShoppingCart shopping = cart.get(0);
            shopping.setNumber(shopping.getNumber() + 1);
            userShoppingMapper.Update(shopping);
        } else {
            //没有相同的菜品
            //没有判断是菜品 还是套餐进行添加
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //添加的是食物
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 添加的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                SetmealVO setmealVO = setmealMapper.SelectByid(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            userShoppingMapper.Insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showcart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cart = userShoppingMapper.Select(shoppingCart);
        return cart;
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        userShoppingMapper.deleteCart(shoppingCart);

    }

    /**
     * 删除一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> cart = userShoppingMapper.Select(shoppingCart);
        ShoppingCart shopping = cart.get(0);
        shopping.setNumber(shopping.getNumber() - 1);
        userShoppingMapper.Update(shopping);
    }
}
